package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerTraceHistoryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTraceHistoryServiceImpl implements ICustomerTraceHistoryService {

    @Autowired
    private CustomerTraceHistoryMapper customerTraceHistoryMapper;

    @Override
    public PageInfo<CustomerTraceHistory> selectByQo(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<CustomerTraceHistory> list = customerTraceHistoryMapper.selectAll(qo);
        PageInfo<CustomerTraceHistory> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void saveOrUpdate(CustomerTraceHistory customerTraceHistory) {
        if(customerTraceHistory.getId()==null){
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            customerTraceHistory.setInputUser(employee);
            customerTraceHistory.setInputTime(new Date());

            customerTraceHistoryMapper.insert(customerTraceHistory);
        }
        customerTraceHistoryMapper.updateByPrimaryKey(customerTraceHistory);
    }
}
